package cn.ctbu.xiaopeng.studentscoremanage.utils;


import cn.ctbu.xiaopeng.studentscoremanage.enums.ResultEnum;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.Result;

/**
 * @author xiaopeng
 * @version 1.0
 */
public class ResultUtils {

    // 成功返回结果，返回类型为 Result<T>
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setData(data);
        result.setMsg("成功");
        return result;
    }

    // 无数据成功返回
    public static <T> Result<T> success() {
        return success(null);
    }

    // 错误返回，带有自定义 code 和 msg
    public static <T> Result<T> error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    // 错误返回，只有 msg
    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.setMsg(msg);
        return result;
    }

    // 错误返回，使用 ResultEnum
    public static <T> Result<T> error(ResultEnum resultEnum) {
        Result<T> result = new Result<>();
        result.setCode(resultEnum.getCode());
        result.setMsg(resultEnum.getMsg());
        return result;
    }
}
